package com.example.viewtest.ActivityGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.example.viewtest.AdapterGroup.HeaderBottomAdapter;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecycleViewActivity extends AppCompatActivity {
    private List<Bean> list = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private HeaderBottomAdapter hbAdapter;
    GridLayoutManager gridLayoutManager;
    LinearLayoutManager layoutManager;
    SwipeRefreshLayout mRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_view);
        //找控件
        mRecyclerView = findViewById(R.id.rv_list);
        mRefreshLayout = findViewById(R.id.refresh_layout);

        //初始化数据
        initRecyclerViewData();

        handlerDownPullUpdate();
        //创建和设置布局管理器
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        //设置适配器
        mRecyclerView.setAdapter(hbAdapter = new HeaderBottomAdapter(this,list));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //获得最后一个可见视图的位置
                int ItemNowPosition = layoutManager.findLastVisibleItemPosition();
                //获得所有Item数量
                int AllItems = layoutManager.getItemCount();
                //获得可见Item数量
                int visibleItem = layoutManager.getChildCount();
                //获得当前RecyclerView的滚动状态
                int state = recyclerView.getScrollState();

                if (visibleItem > 0 && ItemNowPosition == AllItems - 1 && state == RecyclerView.SCROLL_STATE_IDLE){
                    loadMore();
                }
            }
        });
//        //Grid布局
//        gridLayoutManager=new GridLayoutManager(RecycleView.this, 2);//2代表一行放2个view
//        mRecyclerView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
//        mRecyclerView.setAdapter(hbAdapter=new HeaderBottomAdapter(this,list));
//
//
//            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//                @Override
//                public int getSpanSize(int position) {
//                    return (hbAdapter.isHeaderView(position) || hbAdapter.isBottomView(position)) ? gridLayoutManager.getSpanCount() : 1;
//                }
//            });
    }

    private void handlerDownPullUpdate() {
        //使刷新方法可用
        mRefreshLayout.setEnabled(true);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //执行刷新数据操作
                //在顶部进行下拉操作时该方法会被触发
                //但该方法是主线程的，不可执行耗时操作,建议 使用子线程
                Bean data = new Bean();
                data.setName("下拉刷新添加的数据");
                data.setImageId(R.drawable.game3);
                list.add(0,data);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //全局刷新
                        hbAdapter.notifyDataSetChanged();
                        //停止数据刷新
                        mRefreshLayout.setRefreshing(false);
                    }
                },1000);
            }
        });
    }


    private int mLastVisibleItemPosition;
    private RecyclerView.OnScrollListener monScrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            }
            if (hbAdapter != null) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && mLastVisibleItemPosition + 1 == hbAdapter.getItemCount()) {
                    Log.d("滚动监听","LastItem位置: "+ mLastVisibleItemPosition + "当前状态: " + newState);
                    loadMore();
                    Log.d("加载情况：","完成加载方法");
                }
            }
        }


    };

    private void loadMore(){
        for(int i=0;i<3;i++) {
            Bean data = new Bean();
            data.setName("下拉刷新添加的数据"+i);
            data.setImageId(R.drawable.game3);
            list.add(data);
            Log.d("新增数据：",data.getName());
        }
        hbAdapter.notifyDataSetChanged();
    }

    public void initRecyclerViewData(){
        Random random = new Random();
        for (int i=0;i<30;i++){
            Bean bean = new Bean();
            bean.setName("Item" + i);
//            bean.setType((random.nextInt()*2)+3);//[3,5)
//            Log.d("随机数生成监控","-----: "+bean.getType());
            switch (i%8){
                case 1:
                    bean.setImageId(R.drawable.game1);
                    break;
                case 2:
                    bean.setImageId(R.drawable.game2);
                    break;
                case 3:
                    bean.setImageId(R.drawable.game3);
                    break;
                case 4:
                    bean.setImageId(R.drawable.landscape1);
                    break;
                case 5:
                    bean.setImageId(R.drawable.landscape2);
                    break;
                case 6:
                    bean.setImageId(R.drawable.landscape3);
                    break;
                case 7:
                    bean.setImageId(R.drawable.landscape4);
                    break;
                case 0:
                    bean.setImageId(R.drawable.landscape5);
                    break;
            }
            list.add(bean);
        }
    }
}